<?php


namespace App\Http\Controllers\Sale;


use App\Models\SaleUser;
use App\Models\SaleUserLogin;
use App\Models\SaleLog;
use App\Models\SaleAuthRule;
use App\Services\CommonService;
use App\Services\SmsService;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class AuthController extends BaseController
// class AuthController
{

    /**
     * 获取用户菜单
     * @param Request $request
     * @return false|string
     */
    public function getAuthRule(Request $request){
        $data = array(
                'router'=>'root',
                // 'children'=>array(
                //         array( // 成员管理
                //             'router'=>'extension',
                //             'children'=>array(
                //                 'approval',
                //                 'member',
                //                 // 'accounts',
                //                 'personal'
                //             )  
                //         ),
                //         array( //订单管理
                //             'router'=>'order',
                //             'children'=>array(
                //                 'OrderList',
                //                 'OverdueList'
                //             )  
                //         ),
                //         array( //推广管理
                //             'router'=>'goods',
                //             'children'=>array(
                //                 'goodslist',
                //                 'channel'
                //             )  
                //         ),
                //         array( //财务管理
                //             'router'=>'account',
                //             'children'=>array(
                //                 'my','Withdrawal','settlement',
                //                 'withdraw'
                //             )  
                //         )
                //     ),
            );
        
        $data['children'] = obToArray(SaleAuthRule::where('pid',0)->where('status',SaleAuthRule::SHOW_ON)->select('path as router','id')->get());
        foreach($data['children'] as $key=>$value){
            $data['children'][$key]['children'] = array_column(obToArray(SaleAuthRule::where('pid',$value['id'])
                                            ->where('status',SaleAuthRule::SHOW_ON)
                                            ->where('role',">=",$this->sale_user['level'])
                                            ->select('path')
                                            ->get()),'path');
            unset($data['children'][$key]['id']);
        }
        // dd($data);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$data);
    }


    /**
     * 修改密码
     * @param Request $request
     * @return false|string
     */
    public function editPwd(Request $request){
        $code = $request->input('code','');
        $pwd = $request->input('pwd','');
        if(!$code || !$pwd){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        $checkCode = SmsService::checkCode($this->users['mobile'],$code);
        if(!$checkCode){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'验证码错误');
        }
        $data['password'] = md5($this->users['mobile'].$pwd);
        $res = SaleUserLogin::where(['id'=>$this->users['id']])->update($data);
        if($res){
            //添加操作记录
            $data = [];
            $data['sale_user_id'] = $this->users['sale_user_id'];
            $data['user_id'] = $this->users['id'];
            $data['content'] = '分销商'.$this->users['id'].'后台修改密码';
            $data['remark'] = '';
            SaleLog::addLog($data);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
        }
        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'操作失败');
    }


}
